package com.dlwx.parttimetalent.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.adapter.WorkExperienceAdapter;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.bean.WorksBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.inter.DialogInter;
import com.dlwx.parttimetalent.inter.PositionInter;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.dlwx.parttimetalent.views.WorkExperienceDialog;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WorkExperienceActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.title_right_one_tv)
    TextView titleRightOneTv;
    private ArrayList<WorksBean.BodyBean> mDatas;
    private WorkExperienceAdapter mAdapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_experience);
        ButterKnife.bind(this);
        initView();
        getDatas();
    }

    private void getDatas() {
        loadingDialog.show();
        OkgoUtil.post(Http.JobList, WorkExperienceActivity.this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        WorksBean parentBean = DealResult.getInstance().dealDatas(WorkExperienceActivity.this, response, new TypeToken<WorksBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS||parentBean.getCode()==Constant.EMPTY) {
                            List<WorksBean.BodyBean> beans = parentBean.getBody();
                            setDatas(beans);
                        }else{
                            ToastUtils.toastForShort(WorkExperienceActivity.this, parentBean.getResult());
                        }
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        recyclerView.refreshComplete(mAdapter.getItemCount());
                        ToastUtils.toastForShort(WorkExperienceActivity.this, getString(R.string.bad_net));
                    }
                });
    }

    private void setDatas(List<WorksBean.BodyBean> beans) {
        if(!beans.isEmpty()){
            for (WorksBean.BodyBean bean : beans) {
                bean.setType(0);
            }
        }
        WorksBean.BodyBean bean=new WorksBean.BodyBean();
        bean.setJob_info("");
        beans.add(bean);
        bean.setType(1);
        mAdapter.addAll(beans);
        recyclerView.refreshComplete(mAdapter.getItemCount());
    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("工作经历");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setAutoMeasureEnabled(true);
        recyclerView.setLayoutManager(manager);
        mAdapter = new WorkExperienceAdapter(this, new PositionInter() {
            @Override
            public void callBack(int position) {
                    WorkExperienceDialog dialog = new WorkExperienceDialog(WorkExperienceActivity.this, new DialogInter() {
                        @Override
                        public void getInfo(String data) {}
                        @Override
                        public void getWorkInfo(String start, String end, String unit) {
                            saveInfo(start, end, unit);
                        }
                    });
                    dialog.show();
            }
        });
        mDatas = new ArrayList<WorksBean.BodyBean>();
        mAdapter.setDataList(mDatas);
         lRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        //divider
        DividerDecoration divider = new DividerDecoration.Builder(this)
                .setHeight(R.dimen.size_4)
//                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.white)
                .build();
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(divider);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        recyclerView.setLoadMoreEnabled(false);
        recyclerView.setPullRefreshEnabled(false);
        mAdapter.setOnDelListener(new WorkExperienceAdapter.onSwipeListener() {
            @Override
            public void onDel(int pos) {
                deleteWork(pos);
            }
            @Override
            public void onTop(int pos) {}
        });
    }

    private void deleteWork(final int pos) {
        loadingDialog.show();
        int jobId=mAdapter.getDataList().get(pos).getJobid();
        OkgoUtil.post(Http.JobDel, WorkExperienceActivity.this)
                .params("jobid",jobId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean = DealResult.getInstance().dealDatas(WorkExperienceActivity.this, response, new TypeToken<BaseBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                            mAdapter.getDataList().remove(pos);
                            mAdapter.notifyItemRemoved(pos);//推荐用这个
                            if(pos != (mAdapter.getDataList().size())){ // 如果移除的是最后一个，忽略 注意：这里的mDataAdapter.getDataList()不需要-1，因为上面已经-1了
                                mAdapter.notifyItemRangeChanged(pos, mAdapter.getDataList().size() - pos);
                            }
                        }else{
                            ToastUtils.toastForShort(WorkExperienceActivity.this, parentBean.getResult());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(WorkExperienceActivity.this, getString(R.string.bad_net));
                    }
                });
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_middle_title, R.id.tv_confirm,R.id.title_right_one_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_middle_title:
                break;
            case R.id.title_right_one_tv:

                break;
            case R.id.tv_confirm:

                break;
        }
    }

    private void saveInfo(String start, String end, String unit) {
        loadingDialog.show();
        OkgoUtil.post(Http.AddJob, WorkExperienceActivity.this)
                .params("start", start)
                .params("end", end)
                .params("job_info", unit)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean = DealResult.getInstance().dealDatas(WorkExperienceActivity.this, response, new TypeToken<BaseBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                            mAdapter.clear();
                            lRecyclerViewAdapter.notifyDataSetChanged();//必须调用此方法
                            getDatas();
                        } else {
                            ToastUtils.toastForShort(WorkExperienceActivity.this, parentBean.getResult());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(WorkExperienceActivity.this, getString(R.string.bad_net));
                    }
                });
    }

//    private void refresh(){
//        mAdapter.clear();
//
//    }
}
